package cn.iocoder.yudao.module.media.service.macvod;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.media.controller.admin.macvod.vo.*;
import cn.iocoder.yudao.module.media.dal.dataobject.macvod.MacVodDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.media.dal.mysql.macvod.MacVodMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.media.enums.ErrorCodeConstants.*;

/**
 * 电影信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MacVodServiceImpl implements MacVodService {

    @Resource
    private MacVodMapper macVodMapper;

    @Override
    public Long createMacVod(MacVodSaveReqVO createReqVO) {
        // 插入
        MacVodDO macVod = BeanUtils.toBean(createReqVO, MacVodDO.class);
        macVodMapper.insert(macVod);
        // 返回
        return macVod.getId();
    }

    @Override
    public void updateMacVod(MacVodSaveReqVO updateReqVO) {
        // 校验存在
        validateMacVodExists(updateReqVO.getId());
        // 更新
        MacVodDO updateObj = BeanUtils.toBean(updateReqVO, MacVodDO.class);
        macVodMapper.updateById(updateObj);
    }

    @Override
    public void deleteMacVod(Long id) {
        // 校验存在
        validateMacVodExists(id);
        // 删除
        macVodMapper.deleteById(id);
    }

    private void validateMacVodExists(Long id) {
        if (macVodMapper.selectById(id) == null) {
            throw exception(MAC_VOD_NOT_EXISTS);
        }
    }

    @Override
    public MacVodDO getMacVod(Long id) {
        return macVodMapper.selectById(id);
    }

    @Override
    public PageResult<MacVodDO> getMacVodPage(MacVodPageReqVO pageReqVO) {
        return macVodMapper.selectPage(pageReqVO);
    }

}